package com.dollar.ruyuan_juc.diy;

import com.dollar.ruyuan_juc.producerConsumer.RequestQueue;

import java.util.concurrent.atomic.AtomicInteger;

public class TestMyBlockingQueue {
    public static void main(String[] args) {
        MyArrayBlockingQueue<String> requestQueue = new MyArrayBlockingQueue(100);
        AtomicInteger atomicInteger = new AtomicInteger(0);

        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    requestQueue.put(atomicInteger.getAndAdd(1) + "");
                }
            }, "producer-" + i).start();
        }

        for (int i = 0; i < 2; i++) {
            new Thread(() -> {
                while (true) {
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "consume: " + requestQueue.poll());
                }
            }, "consumer-" + i).start();
        }
    }
}
